import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.List;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class RasoloFisher extends Script implements PaintListener,
		ServerMessageListener { // Names the script

	int runAt = random(20, 100);

	public long startTime = System.currentTimeMillis();

	int Fishgotten;
	int loads;
	private int startLvl = 0;
	private int startExp = 0;

	RSTile[] ExtraPath = { new RSTile(2534, 3423) };

	RSTile[] BackPath = { new RSTile(2535, 3431), new RSTile(2533, 3422),
			new RSTile(2529, 3414) };

	public boolean atRasolo() {
		final RSNPC RasoloDistance = getNearestNPCByID(1972);
		return distanceTo(RasoloDistance.getLocation()) <= 5;
	}

	private boolean clickNPC(final RSNPC npc, final String action) {
		if (npc == null) {
			return false;
		}
		final RSTile tile = npc.getLocation();
		if (!tile.isValid()) {
			return false;
		}

		try {
			Point screenLoc = npc.getScreenLocation();
			if (distanceTo(tile) > 6 || !pointOnScreen(screenLoc)) {
				turnToTile(tile);
			}
			if (distanceTo(tile) > 20) {
				walkTileMM(tile);
				return false;
			}
			for (int i = 0; i < 12; i++) {
				screenLoc = npc.getScreenLocation();
				if (!npc.isValid() || !pointOnScreen(screenLoc)) {
					return false;
				}
				moveMouse(screenLoc, 5, 5);
				if (getMenuItems().get(0).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					break;
				}
				if (getMouseLocation().equals(screenLoc)) {
					break;
				}
			}
			final List<String> menuItems = getMenuItems();
			if (menuItems.isEmpty()) {
				return false;
			}
			for (int a = 0; a < menuItems.size(); a++) {
				if (menuItems.get(a).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					if (menuItems.get(0).toLowerCase().contains(
							action.toLowerCase())) {
						clickMouse(true);
						return true;
					} else {
						clickMouse(false);
						return atMenu(action);
					}
				}
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public String getAuthor() { // Sets the Authors name
		return "Hellfish";
	}

	@Override
	public String getName() { // Sets the Scripts name
		return "RasoloFisher";
	}

	@Override
	public String getScriptCategory() { // Sets the category that the script
		// fit's into
		return "Fishing";
	}

	@Override
	public String getScriptDescription() { // Shows a description of the script
		// inside the script dump area
		String html = "<html>";
		html += "<body>";
		html += "</body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() { // Sets the Version number of the script
		return 1.1;
	}

	@Override
	public int loop() { // This is the area of the script where you code out the
		// things you want the bot to do

		if (getEnergy() >= runAt && !isRunning()) {
			setRun(true);
			wait(3000);
			runAt = random(20, 100);
			return random(10, 50);
		}

		// -------TRADING PART-------

		if (RSInterface.getInterface(620).isValid()) {
			atInventoryItem(331, "Sell 50");
			atInventoryItem(331, "Sell 50");
			wait(1000);
			atInventoryItem(335, "Sell 50");
			atInventoryItem(335, "Sell 50");
			loads++;
		}
		if (!isInventoryFull()) {
			walkPathMM(randomizePath(BackPath, 2, 2), 14);
			wait(1000);
		}

		if (isInventoryFull()) {

			final RSNPC Rasolo = getNearestNPCByID(1972);

			if (Rasolo != null) {

				if (!getMyPlayer().isMoving()) {
					if (!RSInterface.getInterface(620).isValid()) {
						if (!atRasolo()) {
							walkTileMM(Rasolo.getLocation());

						}
					}
				}
				clickNPC(Rasolo, "Trade");
				wait(300);
			}

			if (Rasolo == null && distanceTo(new RSTile(2531, 3412)) <= 6) {
				walkPathMM(randomizePath(ExtraPath, 2, 2), 15);
			}
		}

		// -------TRADING PART-------

		// -------FISHING PART-------

		if (!getMyPlayer().isMoving() && getMyPlayer().getAnimation() == -1) {

			final RSNPC Spot = getNearestNPCByID(310);

			if (Spot != null && !isInventoryFull()) {

				clickNPC(Spot, "Lure");

				wait(2000);

			}
		}
		// -------FISHING PART-------
		return random(100, 150);
	}

	@Override
	public void onFinish() { // Loads any commands to do when the script is
		// ended
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {

		long millis = System.currentTimeMillis() - startTime;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		if (startExp == 0) {
			startExp = skills.getCurrentSkillExp(10);
		}
		if (startLvl == 0) {
			startLvl = skills.getCurrentSkillLevel(10);
		}

		g.setColor(Color.green);

		g.setColor(Color.YELLOW);
		g.drawString("Hellfish's RasoloFisher v1.2", 320, 260);
		g.setColor(Color.RED);
		g.drawString("run: " + hours + " hrs " + minutes + " min ", 320, 275);
		g.drawString("Catches: " + Fishgotten, 320, 290);
		g.drawString("Loads: " + loads, 320, 305);

		g.drawString("Levels Gained: "
				+ (skills.getCurrentSkillLevel(10) - startLvl), 50, 260);
		g.drawString("Experience Earned: "
				+ (skills.getCurrentSkillExp(10) - startExp), 50, 275);
		g.drawString("Current Level: " + skills.getCurrentSkillLevel(10), 50,
				290);
		g.drawString("Exp to next level: " + skills.getXPToNextLevel(10), 50,
				305);

	}

	@Override
	public boolean onStart(final Map<String, String> args) { // Loads any
		// commands
		// needed when the
		// script is started
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		if (e.getMessage().contains("You catch")) {
			Fishgotten++;

		}
	}

}